<template>
  <!--  <h2>生产院专业对照</h2>-->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch">
      <el-form-item label="生产院" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择" style="width: 240px"></treeselect>
      </el-form-item>
      <el-form-item label="专业名称" prop="majorName">
        <el-input v-model="queryParams.majorName" placeholder="请输入专业名称" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="专业代码" prop="majorCode">
        <el-input v-model="queryParams.majorCode" placeholder="请输入专业名称" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" ref="refbut">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus"  @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableDataList" @selection-change="handleSelectionChange" :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="majorId"/>
      <el-table-column label="生产院名称" prop="deptName" align="center" />
      <el-table-column label="专业名称" prop="majorName" align="center" />
      <el-table-column label="专业代码" prop="majorCode" align="center" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button  type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button  type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加/修改文件 -->
    <el-dialog :title="title" :visible.sync="open" width="35%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="生产院" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择" style="width: 100%"></treeselect>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="专业名称" prop="majorName">
              <el-input v-model="form.majorName" placeholder="请输入专业名称" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="专业代码" prop="majorCode">
              <el-input v-model="form.majorCode" placeholder="请输入专业代码" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" style="float: left; margin-left: 5%" @click="append">继承追加</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button  @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addSpeciality, deleteSpeciality,
  getDetailById,
  getTableList,
  updateSpeciality
} from "@/api/stage/stageManagement/professionalComparison";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {deptTreeSelect} from "@/api/system/user";
import estimateTableHeight from "@/mixins/estimateTableHeight";

export default {
  name: "index",
  components: { Treeselect },
  mixins: [estimateTableHeight], // 混入方法
  data() {
    return {
      queryParams: {
        pageNum:1,
        pageSize:10,
        majorName: undefined,
        majorCode: undefined,
        deptId: undefined
      },
      // 部门树选项
      deptOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 遮挡层
      loading: true,
      // 表格数据
      tableDataList: [],
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      title: '',
      open: false,
      form: {
        majorName: undefined,
        majorCode: undefined,
        deptId: undefined
      },
      rules: {
        majorName: [
          { required: true, message: "专业名称不能为空", trigger: "blur" }
        ],
        majorCode: [
          { required: true, message: "专业代码不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "生产院不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getDeptTreePost()
    this.getList()
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTreePost() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true
      getTableList(this.queryParams).then(response=>{
        this.loading = false
        this.tableDataList = response.rows
        this.total = response.total
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    table_index(index){
      return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
    },
    // 增加项目
    handleAdd() {
      this.resetForm("form");
      this.title = '添加'
      this.open = true
    },
    // 修改
    handleUpdate(row) {
      this.resetForm("form");
      getDetailById({majorId: row.majorId}).then(response=>{
        this.form = response.data
        this.title = '修改'
        this.open = true
      })
    },
    append() {
      this.$refs["form"].validate(valid => {
          if(valid) {
            addSpeciality(this.form).then(response=>{
              this.$modal.msgSuccess('追加成功')
              this.getList()
            })
          }
      })
    },
    // 保存
    submitForm() {
      this.$refs["form"].validate(valid => {
        if(valid) {
          if(this.form.majorId != undefined) {
            updateSpeciality(this.form).then(response=>{
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSpeciality(this.form).then(response=>{
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 取消
    cancel() {
      this.open = false
    },
    // 删除
    handleDelete(row) {
      console.log(row)
      const majorId = row.majorId
      this.$modal.confirm('是否确认删除专业名称为"' + row.majorName + '"的数据项？').then(function() {
        return deleteSpeciality({majorId: majorId})
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
}
</script>

<style scoped>

</style>
